//
//  LandmarkDetail.swift
//  SwiftDemo
//
//  Created by changlei on 2020/11/13.
//

import SwiftUI

struct LandmarkDetail: View {
    @EnvironmentObject var userData: UserData
    var landmark: Landmark

    var landmarkIndex: Int {
        userData.landmarks.firstIndex(where: { $0.id == landmark.id })!
    }

    var body: some View {
        VStack {
            MapView(coordinate: landmark.locationCoordinate)
                    .edgesIgnoringSafeArea(.top)
                    .frame(height: 300)

            CircleImage(image: landmark.image)
                    .offset(x: 0, y: -130)
                    .padding(.bottom, -130)

            VStack(alignment: .leading) {
                HStack {
                    Text(landmark.name)
                            .font(.title)

                    Button(action: {
                        userData.landmarks[landmarkIndex].isFavorite.toggle()
                    }) {
                        if userData.landmarks[landmarkIndex].isFavorite {
                            Image(systemName: "star.fill")
                                    .foregroundColor(.yellow)
                        } else {
                            Image(systemName: "star")
                                    .foregroundColor(.gray)
                        }
                    }
                }
                HStack(alignment: .top) {
                    Text(landmark.park)
                            .font(.subheadline)
                    Spacer()
                    Text(landmark.state)
                            .font(.subheadline)
                }
            }
                    .padding()
            Spacer()
        }
                .navigationBarTitle(Text(landmark.name), displayMode: .inline)
    }
}

struct LandmarkDetail_Previews: PreviewProvider {
    static var previews: some View {
        LandmarkDetail(landmark: landmarkData[0])
                .environmentObject(UserData())
    }
}
